// ==UserScript==
// @name      [kesai]谷歌百度集合
// @namespace  
// @author    kesai
// @version    1.1.1
// @description  分别在百度和google的搜索结果页面增加搜索跳转按钮，google搜索页面提供google翻译跳转。顺便推荐一波在线youtube视频下载的工具： https://www.findyoutube.net
// @icon https://resource01.ulifestyle.com.hk/res/v3/image/content/2130000/2132767/20180807_googlebaidu_03_1024.jpg
// @include        https://www.google.com.hk/*
// @include        http://www.google.com.hk/*
// @include        https://ipv6.google.com.hk/*
// @include        http://ipv6.google.com.hk/*
// @include        http://www.google.com/*
// @include        https://www.google.com/*
// @include        http://ipv4.google.com/*
// @include        https://ipv4.google.com/*
// @include        http://ipv6.google.com/*
// @include        https://ipv6.google.com/*
// @include        http://www.baidu.com/*
// @include        https://www.baidu.com/*
// @copyright      2018+, FindHao
// @grant          none

// ==/UserScript==


if ("undefined" == typeof(jQuery)) {
    loadJs("for_google", "https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js", callbackFunction);
} else {
    $(document).ready(function() {
        function baiduswitchgoogle() {
            $('.s_btn_wr').replaceWith('<span class="bg s_btn_wr"><input type="image" id="su"  src="" style="height:30px;padding-right: 10px;padding-left: 10px;"></span>');

            $('.s_btn_wr').after('<span class="bg s_btn_wr"><input type="image" id="ggyx"  src="" style="height:30px;padding-right: 10px;padding-left: 10px;" ></span>');
            $('#ggyx').on({
                click: function() {
                    window.open("https://www.google.com/ncr?gws_rd=ssl#newwindow=1&q=" + $('#kw').val());
                    return false;
                }
            });
        }

        if (window.location.search.lastIndexOf("wd=") > 0 || window.location.search.lastIndexOf("word=") > 0) {
            baiduswitchgoogle();
        }
        //检测从baidu首页进入的搜索（补漏）
        if (/^http(s)?:\/\/(www\.)?baidu\.com\/$/ig.test(window.location.href)) {
            $("#kw").off('click').on({
                keydown: function() {
                    if ($('#ggyx').length < 1 && $('#kw').val().length > 0) { baiduswitchgoogle(); }
                }
            }).on({
                paste: function() {
                    if ($('#ggyx').length < 1) { baiduswitchgoogle(); }
                }
            });
        }
    });
}

function callbackFunction() {
    $(document).ready(function() {
        function googleswitchbaidu() {
            var baiduButton = $('<div id="bdyx" class="dRYYxd"><div jscontroller="MC8mtf" class="JC0tCe" title="切换到百度搜索" aria-label="切换到百度搜索" role="button" tabindex="0" ><span style="background:url() 0 0 no-repeat;width:24px;height:24px;background-size: 24px;"></span></div></div>');
            if ($('#sblsbb').length > 0) {
                $('#sblsbb').after(baiduButton);
                $('#bdyx').off('click').on({
                    click: function() {
                        window.open("https://www.baidu.com/s?wd=" + $('#lst-ib').val());
                        return false;
                    }
                });
            } else if ($('._wtf').length > 0) {
                $('._wtf').after(baiduButton);
                $('#bdyx').off('click').on({
                    click: function() {
                        window.open("https://www.baidu.com/s?wd=" + $('#lst-ib').val());
                        return false;
                    }
                });
            } else if ($(".Tg7LZd").length > 0) {
                $('.Tg7LZd').after(baiduButton);
                $('#bdyx').off('click').on({
                    click: function() {
                        window.open("https://www.baidu.com/s?wd=" + $(".gLFyf")[0].value);
                        return false;
                    }
                });
            } else {
                $('.sbico').after(baiduButton);
                $('#bdyx').off('click').on({
                    click: function() {
                        window.open("https://www.baidu.com/s?wd=" + $('#lst-ib').val());
                        return false;
                    }
                });
            }

        }
        if (window.location.hash.lastIndexOf("q=") > 0 || window.location.search.lastIndexOf("q=") > 0) {
            addTranslate();
            if ($(".hdtb-msel").text() === "全部")
                googleswitchbaidu();
        }
        //检测Google的直接搜索（补漏）
        if (/(www\.|ipv4\.)?google\.com/ig.test(window.location.href)) {
            $("#lst-ib").off('click').on({
                keydown: function() {
                    if ($('#bdyx').length < 1) { googleswitchbaidu(); }
                }
            });
        }
    });
}

function loadJs(sid, jsurl, callback) {
    var nodeHead = document.getElementsByTagName('head')[0];
    var nodeScript = null;
    if (document.getElementById(sid) === null) {
        nodeScript = document.createElement('script');
        nodeScript.setAttribute('type', 'text/javascript');
        nodeScript.setAttribute('src', jsurl);
        nodeScript.setAttribute('id', sid);
        if (callback !== null) {
            nodeScript.onload = nodeScript.onreadystatechange = function() {
                if (nodeScript.ready) {
                    return false;
                }
                if (!nodeScript.readyState || nodeScript.readyState == "loaded" || nodeScript.readyState == 'complete') {
                    nodeScript.ready = true;
                    callback();
                }
            };
        }
        nodeHead.appendChild(nodeScript);
    } else {
        if (callback !== null) {
            callback();
        }
    }
}

function addTranslate() {
    //增加切换翻译功能
    var q = $(".gLFyf").val();
    var url = "https://translate.google.com/?newwindow=1&um=1&ie=UTF-8&hl=zh-CN&client=tw-ob#view=home&op=translate&sl=auto&tl=en&text=" + q;
    var div = $('<div aria-selected="false" class="hdtb-mitem hdtb-imb" role="tab"><a class="q qs" href="' + url + '"><span class="HF9Klc ZYMsjf" style="height:16px;width:16px;opacity:0.8;"><img src="https://img.icons8.com/material/16/000000/google-translate.png"></span>翻译</a></div>');
    $('#hdtb-msb-vis').append(div);
    $(".g").css("width","1080px!important");
}